package com.hmall.order.Handler;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;


/**
 * @author Lewis
 * @date 2023/4/9 16:59
 * description 公共字段自动填充
 */

@Slf4j
@Component
public class OrderMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入操作，自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("MP公共字段开始填充(insert)：-->");
        metaObject.setValue("createTime", DateTime.now());
        metaObject.setValue("updateTime", DateTime.now());
    }


    /**
     * 更新操作，自动填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("MP公共字段开始填充(update)：-->");
        try {
            if(metaObject.hasSetter("updateTime")){
                metaObject.setValue("updateTime", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        metaObject.setValue("updateTime", DateTime.now());
    }
}
